﻿// asumes existence of:
// Model.lists

// Used with duration and money
function Unit()
{
    this.getControls = function (prop)
    {
        var options = $.jqote("<option value='<%=j%>'><%=this%></option>", durationUnits);

        return stringFormat("<div class='Duration'>\
                                    <input type='text' id='{0}' name='{0}' {3} class='Numeric'/>\
                                    <select id='{1}' name='{1}' {3}>{2}</select>\
                                 </div>"
                                 , prop.Name + ".Value"
                                 , prop.Name + (prop.Class = "Duration" ? ".Unit" : ".Currency")
                                 , options
                                 , prop.Editable == false ? "disabled='disabled'" : "");
    };

    this.setValue = function (value, prop)
    {
        var type = prop.Class = "Duration" ? "Unit" : "Currency";
        value = value || {};
        $(stringFormat('[name="{0}.Value"]', prop.Name)).val(value.Value);
        $(stringFormat('[name="{0}.{1}"]', prop.Name, type)).attr('value', value[type]);
    };
}

function Resource()
{
    this.getControls = function (prop)
    {
        return stringFormat("<div class='Resource'>\
                                    <div id='{0}' name='{0}', class='{1}' url='{2}'></div>\
                                 </div>"
                                 , prop.Name
                                 , prop.Class
                                 , prop.Url);
    };

    this.setValue = function (values, prop)
    {
        var el = $(stringFormat('[name="{0}"]', prop.Name));
        el.tokenInput("clear");
        for (var i = 0; values && i < values.length; i++)
            el.tokenInput("add", { id: values[i].ID.Value, name: values[i].Name });
    };
}

function Entity()
{
    this.getControls = function (prop)
    {
        var options = $.jqote("<option value='<%=this.Key%>'><%=this.Value%></option>",
                                Model.lists[prop.List]);

        return stringFormat("<div class='Entity'>\
                                    <select id='{1}' name='{1}' {4}>{2}</select>\
                                    <input type='hidden' id='{0}' name='{0}' value='{3}'  {4}/>\
                                 </div>"
                                 , prop.Name + ".Id.TypeName"
                                 , prop.Name + ".Id.Value"
                                 , options
                                 , prop.List
                                 , prop.Editable == false ? "disabled='disabled'" : "");
    };

    this.setValue = function (value, prop)
    {
        $(stringFormat('[name="{0}.Id.Value"]', prop.Name)).attr('value', value.Id.Value);
    };
}

function EntitySearch()
{
    this.getControls = function (prop)
    {
        return stringFormat("<div class='EntitySearch' type_name='{4}'>\
                                    <select id='{1}' name='{1}' {3}></select>\
                                    <input type='hidden' id='{0}' name='{0}' value='{2}'/>\
                                 </div>"
                                 , prop.Name + ".Id.TypeName"
                                 , prop.Name + ".Id.Value"
                                 , prop.List
                                 , prop.Editable == false ? "disabled='disabled'" : ""
                                 , prop.TypeName);
    };

    this.setValue = function (value, prop)
    {
        $(stringFormat('[name="{0}.Id.Value"]', prop.Name)).html($.jqote("<option value='<%=this.Id.Value%>'><%=this.Values[0].Value%></option>", value));
    };
}

function Text()
{
    this.getControls = function (prop)
    {
        if (prop.Editable == false)
            return stringFormat("<div class='Text' id='{0}' name='{0}'>\
                                 </div>"
                                 , prop.Name);

        else return stringFormat("<div class='Text'>\
                                    <input type='text' id='{0}' name='{0}' class='{1}'/>\
                                 </div>"
                                 , prop.Name
                                 , prop.Class);
    };

    this.setValue = function (value, prop)
    {
        var el = $(stringFormat('[name="{0}"]', prop.Name));
        if (prop.Editable == false)
            el.html(value);
        else
            el.val(value);
    };
}

function Link()
{
    this.getControls = function (prop)
    {
        return stringFormat("<div class='Link' onclick='{1}'>\
                                    {0}\
                             </div>"
                                 , prop.Text
                                 , prop.onclick);
    };

    this.setValue = function (value, prop)
    {
    };
}